#1. 数据分布使用hist函数依次画出RNAseq数据的直方图（总体分布）。
#count数据
rownames(count) <- count$gene_id
count <- count[,-1]
histdata <- count[1,]
histdata <- as.numeric(histdata)
histdata <- unlist(histdata)
hist(histdata,breaks = 30,
     main = "Histogram of TSPAN6 gene",
     xlab = "count")

#clin数据
histdata <- clin[,6]
histdata <- unlist(histdata)  #unlist()函数的作用是将一个列表（list）或向量组合结构 "展开" 为一个简单的向量
hist(histdata,breaks = 20,
     main = "age",
     xlab = "clin")

#exp数据
histdata <- clin[,4]
histdata <- unlist(histdata)  #unlist()函数的作用是将一个列表（list）或向量组合结构 "展开" 为一个简单的向量
hist(histdata,breaks = 20,
     main = "X28s.X18",
     xlab = "exp")



#2. 对ADdata进行层次聚类分析，画出树状图

#基因的层次聚类
distance <- dist(ADdata,method = "euclidean") #计算距离  
clust <- hclust(distance)
plot(clust,main = "gene clust dendrogram",xlab = "gene",ylab = "distance")
#样本的层次聚类
distance <- dist(t(ADdata),method = "euclidean") #计算距离  
clust <- hclust(distance)
plot(clust,main = "sample clust dendrogram",xlab = "sample",ylab = "distance")



3. 火山图
#在volcano.RData数据中FC为log2 fold change值，P为p值，
#请以 log2 fold change为横坐标，-log10 P 值为纵坐标，
#以P 小于0.05 以及fold change 大于 1.2的数据作为上调蛋白，
#以P 小于0.05 以及fold change 小于 1/1.2的数据作为下调蛋白
#，绘制该数据的火山图，并输出成jpg文件。


volcano <- load("C:/Users/32076/Downloads/volcano.RData")
library(ggplot2)
#创建volcano_data数据框接受更改后的数据，用于直接绘图，并增加diffexp列
volcano_data <- data.frame(
  ID=prostat$ID,FC=prostat$FC,negativelog10p=-log10(prostat$P),diffexp="No")
#给差异表达的蛋白分类
volcano_data$diffexp[volcano_data$negativelog10p > -log10(0.05)&volcano_data$FC > log2(1.2)] <- "Up"
volcano_data$diffexp[volcano_data$negativelog10p > -log10(0.05)&volcano_data$FC < log2(1/1.2)] <- "Down"
volcano_data$diffexp <- factor(volcano_data$diffexp,levels=c("Down","No","Up"))
#绘图
volcano_plot <- ggplot(volcano_data,aes(x=FC,y=negativelog10p,color=diffexp))+
  geom_point(alpha = 0.6, size = 1.5)+   # 调整点的大小和透明度
  scale_color_manual(values = c("blue", "grey", "red"))+   # 设置颜色
  geom_vline(xintercept = c(log2(1/1.2), log2(1.2)), 
             linetype = "dashed", color = "black", linewidth = 0.3)+   # 添加阈值线
  geom_hline(yintercept = -log10(0.05), 
             linetype = "dashed", color = "black", linewidth = 0.3)+   # 添加显著性线
  labs(x = expression(log[2]~Fold~Change), 
       y = expression(-log[10]~P~value),
       color = "Expression")     #设置x，y标签
volcano_plot
#文件保存
ggsave(
  "volcano_plot.jpg",   # 文件名
  volcano_plot,         # 要保存的ggplot对象
  width = 8,            # 图片宽度
  height = 6,           # 图片高度
  dpi = 300,            # 分辨率
)





